<script setup>
import { ref } from 'vue'
// 动画 animate.css
import "animate.css"
// 引入瀑布流布局插件
import { LazyImg,Waterfall } from 'vue-waterfall-plugin-next'
import { getCategoryService, getProductListService } from '@/api/product.js'
import { useUserStore } from '@/stores/index'
import { Star } from '@element-plus/icons-vue'
import ProductDetail from '../product/ProductDetail.vue'
const userStore = useUserStore()
// -------------------------获取商品分类列表-------------------------------
// 当前选中的分类
const selectedCategory = ref(null)
const categoryList = ref([])
const getCategory = async () => {
  const res = await getCategoryService()
  categoryList.value = res.data.data
}
getCategory()
// -------------------------获取商品列表-------------------------------

const getProductBycategory = async (id) => {
  selectedCategory.value = id
  const res = await getProductListService(id)
  productList.value = res.data.data
}
getProductBycategory(selectedCategory.value)

// 商品分类数据
const productList = ref([])
const breakpoints = ref({
  1600: {
    // when wrapper width < 1200
    rowPerView: 5,
  }
})
// ==============================获取商品详情==================================
const showDetail = ref(false)
const productId = ref(null)
const handleDetail = (id) => {
  showDetail.value = true
  productId.value = id
}

</script>
<template>
  <ProductDetail v-if="showDetail" v-model="showDetail" :productId ></ProductDetail>
  <div class="recommend-container animate__animated animate__zoomIn ">
    <div class="category-list">
      <button class="category-list-item" :class="{ 'active-btn': selectedCategory === null }"
        @click="getProductBycategory(null)">全部</button>
      <button class="category-list-item" :class="{ 'active-btn': selectedCategory === category.id }"
        @click="getProductBycategory(category.id)" v-for="category in categoryList" :key="category.id">{{ category.name
        }}</button>
    </div>
    <el-empty v-if="productList[0] == null" :image-size="200" />
    <!-- 瀑布流布局 -->
    <Waterfall v-if="productList.length" :list="productList" :gutter="18" :imgSelector="'coverImage'" :crossOrigin="false"
    :breakpoints :animationEffect="'animate__zoomIn'">
      <template #item="{ item,url }">
        <div class="waterfull-card" @click="handleDetail(item.id)">
          <!-- 封面 -->
          <LazyImg :url="url" style="border-radius: 15px;width: 220px;"   />
          <div class="footer">
            <!-- 标题 -->
            <a class="title"><span>{{ item.title }}</span></a>
            <!-- 价格和发货方式 -->
            <div class="price">
              <span style="color: red; font-weight: 600; font-size: 15px;">￥{{item.price}}</span>
              <span style="font-size: 12px;color: #9e9e9e;">{{ item.postType === 0 ? '邮寄' : '自提' }}</span>
            </div>
            <!-- 头像和想要人数 -->
            <div class="avatar-Like">
              <div class="avatar">
                <img class="avatar-img"
                  :src="item.avatar || 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'"
                  style="width: 25px; border-radius: 50%; outline: none;">
                <span style="margin-left: 5px; color: #5c5c5c; font-size: 12px;">{{item.nickName}}</span>
              </div>
              <div class="like">
                <span>
                  <el-icon style="font-size: 14px; color: #5c5c5c; ">
                    <Star />
                  </el-icon>
                </span>
                <span style="font-size: 12px; color: #5c5c5c;">100+</span>
              </div>
            </div>
          </div>
        </div>
      </template>
    </Waterfall>
  </div>
</template>
<style lang="less" scoped>
.animate__animated {
  animation-fill-mode: both;
  animation-duration: 0.5s;
}

.category-list {
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 100px;
  margin-top: 13px;
  margin-bottom: 10px;

  .category-list-item {
    width: 80px;
    height: 40px;
    background-color: #ffffff;
    font-size: 16px;
    border-radius: 20px;
    display: inline-block;
    list-style: none;

    /* 仅未激活按钮的悬浮样式 */
    &:not(.active-btn):hover {
      background-color: #ebeaea;
      cursor: pointer;
    }
  }

  .active-btn {
    font-weight: 700;
    color: #fff;
    background-color: #fc3e57;
  }
}

.waterfull-card {
  transition: all .4s;
  &:hover{
  cursor: pointer;
    transform: translateY(-10px);
  }

  .footer {
    padding: 10px;

    .title {
      display: block;
      margin-bottom: 5px;
      padding-left: 5px;
    }

    .price {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 5px;
    }

    .avatar-Like {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .avatar {
        display: flex;
        align-items: center;
      }

      .like {
        display: flex;
        align-items: center;
        font-style: 12px;
      }
    }
  }
}
</style>